package net.sppan.base.controller.business;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.sppan.base.common.JsonResult;
import net.sppan.base.common.PageBean;
import net.sppan.base.common.RepairStatusEnum;
import net.sppan.base.controller.BaseController;
import net.sppan.base.entity.Department;
import net.sppan.base.entity.Employee;
import net.sppan.base.entity.business.RepairRecord;
import net.sppan.base.service.DeparmentService;
import net.sppan.base.service.EmployeeService;
import net.sppan.base.service.IRepairRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备维修
 */
@Controller
@RequestMapping("/business/repair")
public class RepairRecordController extends BaseController{
    @Autowired
    private IRepairRecordService repairRecordService;

	@RequestMapping(value ="/index")
	public String index(){
		return "/business/quality/index";
	}

    /**
     * 分页查询-维修记录
     * @param pageSize
     * @param pageNumber
     * @return
     */
	@RequestMapping(value = { "/getRepairRecordList" })
	@ResponseBody
	public PageBean getEmployeeList(@RequestParam("pageSize") Integer pageSize,@RequestParam("pageNumber")Integer pageNumber) {
		Page<RepairRecord> page = PageHelper.startPage(pageNumber, pageSize, true);
        List<RepairRecord> resultList = repairRecordService.getRepairRecord();
        if(!CollectionUtils.isEmpty(resultList)){
            resultList.forEach(repairRecord->{
                repairRecord.setStatusName(RepairStatusEnum.getValueByCode(repairRecord.getStatus()));
            });
        }
		PageBean<RepairRecord> pageBean = new PageBean<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        pageBean.setContent(resultList);
		return pageBean;
	}
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        return "/business/quality/form";
    }

    /**
     * 修改维修记录
     * @param repairRecord
     * @param map
     * @return
     */
    @RequestMapping(value= {"/edit"} ,method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(RepairRecord repairRecord, ModelMap map){
        try {

            repairRecordService.saveRepairRecord(repairRecord);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * 标记已维修
     * @param id
     * @param map
     * @return
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(@PathVariable Long id, ModelMap map) {
        repairRecordService.updateStatus(id,2);
        return JsonResult.success();
    }


    /**
     * 删除维修记录
     * @param id
     * @param map
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable Long id, ModelMap map) {
        repairRecordService.delete(id);
        return JsonResult.success();
    }
}
